#include "CouncilMemberManager.h"

#include <fstream>

const CouncilMember *CouncilMemberManager::memberById(const std::string &id) const
{
  CouncilMemberMap::const_iterator found = m_members.find(id);
  if (found != m_members.end())
    return &((*found).second);
  return 0;
}

bool CouncilMemberManager::serialize( const std::string& fileName ) const
{
  if (fileName.empty())
    return false;
  std::ofstream stream(fileName.c_str());
  toStream(stream);
  return true;
}

std::string CouncilMemberManager::toString() const
{
  std::ostringstream stream;
  toStream(stream);
  return stream.str();
}

void CouncilMemberManager::toStream(std::ostream& stream) const
{
  for (CouncilMemberMap::const_iterator it = m_members.begin(); it != m_members.end(); ++it)
    stream << (*it).second;
}

bool CouncilMemberManager::deserialize( const std::string& fileName )
{
  if (fileName.empty())
    return false;
  m_members.clear();
  std::ifstream stream(fileName.c_str());
  while (!stream.eof())
  {
    CouncilMember member;
    stream >> member; 
    if (!member.empty())
      m_members.insert(CouncilMemberPair(member.id(), member));
  }
  return true;
}
